<template>
	<uni-loading v-if="loading"></uni-loading>
	<view class="body" v-else>
		<!--  顶部  -->
<!-- 		<top-tab :options="stockList" :locationName="locationName" @stockChange="stockChange"
			@handleBarnChoose="handleBarnChoose"></top-tab> -->
		<!--  列表  -->
		<template v-if="purchaseList && purchaseList.length > 0">
			<uni-swipe-action>
				<uni-swipe-action-item :left-options="options" @click="handleDelete(item)" v-for="item in purchaseList">
					<view class="panel" @click="openDetail(item)">
						<view class="panel_title">
							<view class="panel_title_left">{{ item.materialName }}</view>
						</view>
						<view class="panel_content">
							<view class="panel_content">
								<view class="panel_content_row">
									<view class="panel_content_row_item">
										<view class="panel_content_row_item_title">件号：</view>
										<view class="panel_content_row_item_value">{{ item.fspecification }}</view>
									</view>
								</view>
							</view>
							<view class="panel_content_row">
								<view class="panel_content_row_item">
									<view class="panel_content_row_item_title">名称：</view>
									<view class="panel_content_row_item_value">{{ item.fmaterialname }}</view>
								</view>
								<view class="panel_content_row_item">
									<view class="panel_content_row_item_title">仓位：</view>
									<view class="panel_content_row_item_value">{{ item.fsrcStockLocId }}</view>
								</view>
							</view>
						</view>
						<view class="panel_dashboard">
							<view class="panel_dashboard_item">
								<view class="panel_dashboard_item_number success">{{ item.flot }}</view>
								<view class="panel_dashboard_item_value">批号</view>
							</view>
							<view class="panel_dashboard_item">
								<view class="panel_dashboard_item_number error">{{ item.fqty }}{{ item.funitname }}
								</view>
								<view class="panel_dashboard_item_value">重量</view>
							</view>
						</view>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</template>
		<uni-empty-info v-else label="暂无物料信息"></uni-empty-info>

		<!--  底部按钮  -->
		<view class="footerPanel">
			<view class="footerPanel_body">
				<button @click="handleGotoTarget" class="footerBtn save"><i
						class="iconfont icon-jinrumore btn_icon"></i>调拨至
				</button>
				<button @click="handleScanCode" class="footerBtn scan" type="primary"><i
						class="iconfont icon-saomiao btn_icon"></i>扫码
				</button>
			</view>
		</view>

	
		<!-- <MaterialPopup ref="scaningPopup" title="物料入库" type="transfer" @success="initData"></MaterialPopup> -->
		<BarnPopup ref="barnPopup" @success="locationScanningSuccess"></BarnPopup>
		<!-- 扫描托盘 -->
		<tray-popup ref="trayPopupRef" @success="trayScanningScuess"></tray-popup>
		<!-- 获取托盘信息 -->
		<GetTrayTransfer ref="trayRef" @success="getTraySuccess"></GetTrayTransfer>
		
		<!-- 获取托盘信息(扫描仓位后获取) -->
		<GetTrayTransferIron ref="locationTrayRef" @success="getTraySuccess"></GetTrayTransferIron>
	</view>
</template>
<script>
	import topTab from "@/components/top-tab/top-tab"
	import trayPopup from '../../components/popup/trayPopup.vue'
	import GetTrayTransfer from "../pop/get_tray_transfer.vue"
	import GetTrayTransferIron from "../pop/get_tray_transfer_iron.vue"
	// import uniLoading from "../../vBase/components/uni-loading/uni-loading"
	import uniEmptyInfo from "../../vBase/components/uni-empty-info/uni-empty-info"
	// import MaterialPopup from "@/components/popup/materialPopup"
	import BarnPopup from "@/components/popup/barnPopup"
	import{getStockListByUser,
	getTrayAllocation,
	getTransferTemp,
	deleteTransferTemp}  from  '@/utils/api.js'

	export default {
		components: {
			topTab,
			BarnPopup,
			trayPopup,
			GetTrayTransfer,
			uniEmptyInfo,
			GetTrayTransferIron
		},
		data() {
			return {
				loading: false,
				purchaseList: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#FF4E74'
					}
				}],
				trayData:{},
				scaningShow: false,
				scanNo: null,
				isFouce: false,
				stockList: [],
				stockNo: '',
				locationNo: '',
				locationName: '',
				isSource: true
			}
		},
		onShow() {
			this.getStockList();
			this.initData()
		},
		onUnload() {
			uni.$off('deleteItem');
		},
		mounted() {
			// this.getStockList()
		
		},
		methods: {
			//添加成功后返回
			getTraySuccess(){
				console.log('刷新数据')
				this.initData()
				this.getStockList();
			},
			//仓位扫码成功后返回
			locationScanningSuccess(e){
				console.log(e)
				
			},
			//扫码成功后返回
			trayScanningScuess(e){
				let _this = this;
				getTrayAllocation({billNo : e}).then(result => {
					if (result.statusCode == 200){
						//判断是托盘还是合并后的托盘
						let isTray = result.data.isTray;
						if(isTray){
							this.trayData = result.data.data ?? {}
								let param = {
									title: '托盘信息',
									trayData: _this.trayData,
									header : _this.header,
									type : 'pick'
								}
								this.$refs.trayRef.open(param)
						} else{
								let param = {
									title: '托盘信息',
									trayDataList:result.data.data,
									header : _this.header
								}
								this.$refs.locationTrayRef.open(param)
						}
						
						
						
					}
				})
			},
			/** 查询仓库*/
			getStockList() {
				this.loading = true;
				try{
					getStockListByUser({}).then(res =>{
						this.stockList = res.data.data.dataList
						this.stockNo = this.stockList[0].stockNo
					})
				} finally{
					this.loading = false;
				}
		
			},
			/** 初始化数据 */
			initData() {
				this.getDataList()
			},
			

			/** 获取数据 */
			getDataList() {
				getTransferTemp({}).then(res =>{
					this.purchaseList = res.data.data.dataList;
				})

			},

			/** 选择仓位 */
			onLocationChange(location) {
				console.log(location)
				this.locationNo = location.locationNo;
				this.locationName = location.locationName;
			},

			/**
			 * 选择仓库
			 * @param e
			 */
			stockChange(e) {
				this.stockNo = this.stockList[e].stockNo
				this.initData()
			},

			/**
			 * 选择仓位
			 * @param e
			 */
			stockLocation(e) {
				this.locationNo = e.locationNo
				this.locationName = e.locationName
			},

			/**
			 * 选择
			 */
			handleBarnChoose() {
				this.$refs.barnPopup.open();
			},
			
			/** 调拨至 */
			handleGotoTarget() {
				
				uni.navigateTo({
					url:'/pages/storehouse/transferTarget'
				})
			},

			/** 扫码 */
			handleScanCode() {
				this.$refs.trayPopupRef.open()
			},

			/**
			 * 删除物料
			 * @param {Object} e
			 */
			handleDelete(material) {
				const self = this
				uni.showModal({
					title: '提示',
					content: '是否删除物料？',
					success(res) {
						if (res.confirm) {
							deleteTransferTemp({id :material.id}).then(res =>{
										uni.showToast({
											title: '物料删除成功',
											icon: 'none'
										})
										self.initData();
							})
						}
					}
				})
			},

		
		}
	}
</script>
<style lang="scss" scoped>
	@import "../../static/css/common";
</style>
